package com.ruoyi.file.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.file.utils.AliyunOssUtils;

/**
 * 阿里云OSS文件存储服务实现
 * 
 * @author ruoyi
 */
@Service
public class AliyunOssSysFileServiceImpl implements ISysFileService
{
    @Autowired
    private AliyunOssUtils aliyunOssUtils;

    /**
     * 阿里云OSS文件上传接口
     * 
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception
    {
        return aliyunOssUtils.uploadFile(file);
    }

    /**
     * 阿里云OSS文件删除接口
     * 
     * @param fileUrl 文件访问URL
     * @throws Exception
     */
    @Override
    public void deleteFile(String fileUrl) throws Exception
    {
        String fileName = aliyunOssUtils.extractFileNameFromUrl(fileUrl);
        if (fileName != null)
        {
            boolean success = aliyunOssUtils.deleteFile(fileName);
            if (!success)
            {
                throw new RuntimeException("删除OSS文件失败: " + fileUrl);
            }
        }
        else
        {
            throw new RuntimeException("无效的OSS文件URL: " + fileUrl);
        }
    }
}